package org.liuyuantao.webapi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.log4j.Logger;

@Path("myresource")
public class MyResource {
	private static final Logger LOGGER = Logger.getLogger(MyResource.class);

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getIt() {
		return "Got it!";
	}

	@POST
	@Path("b")
	public String postBytes(final byte[] bs) {
		for (final byte b : bs) {
			LOGGER.debug(b);
		}
		return "byte[]:" + new String(bs);
	}

	@POST
	@Path("c")
	public Response postString(final String s) {
		LOGGER.debug(s);
		return Response.ok().entity("char[]:" + s).build();
	}

	@DELETE
	@Path("{s}")
	public void delete(@PathParam("s") final String s) {
		LOGGER.debug(s);
	}

	@POST
	@Path("f")
	public File postFile(final File f) throws IOException {
		try (BufferedReader br = new BufferedReader(new FileReader(f))) {
			String s;
			do {
				s = br.readLine();
				LOGGER.debug(s);
			} while (s != null);
			return f;
		}
	}

	@POST
	@Path("bio")
	public String postStream(final InputStream is) throws IOException {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
			StringBuilder result = new StringBuilder();
			String s = br.readLine();
			while (s != null) {
				result.append(s).append("\n");
				LOGGER.debug(s);
				s = br.readLine();
			}
			return result.toString();
		}
	}

	@POST
	@Path("cio")
	public Response postChars(final Reader r) throws IOException {
		try (BufferedReader br = new BufferedReader(r)) {
			StringBuilder result = new StringBuilder();
			String s = br.readLine();
			if (s == null) {
				throw new RuntimeException("NOT FOUND FROM READER");
			}
			while (s != null) {
				result.append(s).append("\n");
				LOGGER.debug(s);
				s = br.readLine();
			}
			return Response.ok().entity(result.toString()).build();
		}
	}
}